<template>
  <div class="user_page fillcontain" v-loading.body="loading" >

    <div class="user_header" style="margin-top: 5px;">

      <AddUserModel ref="addUserModel"></AddUserModel>

      <el-button type="primary" @click="addUser">添加用户</el-button>

      <el-input placeholder="请输入内容" v-model="inputValue" style="width: 400px; margin-top: 5px; margin-left: 20%">
        <el-select v-model="searchValue" slot="prepend" placeholder="请选择" style="width: 120px">
          <el-option label="餐厅名" value="1"></el-option>
          <el-option label="订单号" value="2"></el-option>
          <el-option label="用户电话" value="3"></el-option>
        </el-select>
        <el-button slot="append" icon="search"></el-button>
      </el-input>
    </div>

    <el-table
      :data="tableData"
      border
      style="width: 100%">

      <el-table-column
        prop="rid"
        label="角色id">
      </el-table-column>

      <el-table-column
        prop="rolename"
        label="角色"
        width="180">
      </el-table-column>

      <el-table-column
        prop="roledesc"
        label="角色描述"
        width="180">
      </el-table-column>

      <el-table-column
        prop="status"
        label="状态"
        width="100">
        <template scope="scope">
          <el-tag
            :type="scope.row.status === 1 ? 'primary' : 'success'"
            close-transition>{{scope.row.status === 1 ? '正常' : '禁用'}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        label="更新日期"
        width="200">
        <template scope="scope">
          <el-icon name="time"></el-icon>
          <span style="margin-left: 10px">{{ scope.row.updatetime }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="创建日期"
        width="200">
        <template scope="scope">
          <el-icon name="time"></el-icon>
          <span style="margin-left: 10px">{{ scope.row.createtime }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="140">
        <template scope="scope">
          <el-button
            type="primary"
            size="small"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>

    <div class="block" style="margin-top: 10px;margin-left: 40%">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentIndex"
        :page-size="10"
        layout="total, prev, pager, next"
        :total="total">
      </el-pagination>
    </div>

  </div>
</template>s

<script>

  import ComSelect from '@/components/common/ComSelect'
  import AddUserModel from '@/components/common/AddUserModel'

  export default {
    components: {
      ComSelect,
      AddUserModel
    },
    data () {
      return {
        inputValue: '',
        searchValue: '',
        currentIndex: 1,
        handleSizeChange: 10,
        total: 10,
        tableData: [],
        loading: true
      }
    },
    methods: {
      //编辑用户
      handleEdit(index, row) {
        console.log(index, row);
      },
      handleDelete(index, row) {
        console.log(index, row);
      },
      //切换页数
      handleCurrentChange () {
        this.loading = true;
        this.getUserList((this.currentIndex - 1) * this.handleSizeChange)
      },
      //获取用户列表
      async getUserList (currentIndex) {
        let users;
        users = await fetch('http://localhost:8081/role/getRoleList/'+ currentIndex + '/' + this.handleSizeChange).then(function(res){
          return res.json();
        });
        console.log(users.data);
        this.tableData = users.data;
        this.loading = false;
      },
      //获取用户数量
      async initData(){
        try{
          const countData = await fetch('http://localhost:8081/role/getRoleCount').then(function(res){
            return res.json();
          });
          if (countData.code === 200) {
            this.getUserList(0);
            this.total = countData.data;
          }else{
            throw new Error('获取数据失败');
          }
        }catch(err){
          console.log('获取数据失败', err);
        }
      },
      //添加用户
      async addUser(){
        var _this=this;
        let title = '添加用户';
        _this.$refs.addUserModel.showModel(title)
      }
    },
    mounted () {

    },
    created () {
      this.initData()
    }
  }
</script>

<style>
  .user_header{
    height: 50px;
    background-color: #F5F5F5;
    margin-bottom: 10px;
    border-radius: 4px;
  }

  .ivu-col-span-4{
    padding-top: 10px;
    padding-left: 15px;
  }

  .ivu-col-span-8{
    padding-top: 10px;
  }

  .ivu-input{
    border-radius: 0px;
    margin-left: -5px;
  }
  .ivu-select-selection{
    border-bottom-right-radius: 0px;
    border-top-right-radius: 0px;
  }

  .searchBtn{
    margin-left: -10px;
    border-bottom-left-radius: 0px;
    border-top-left-radius: 0px;
  }
  .ivu-modal-body .ivu-form-label-left{
    margin-left: 0 !important;
    padding: 0 12%;
    margin-top: 3% !important;
  }
</style>
